package k3_IO.p2_fileStream.demo01;

import java.io.FileReader;
import java.io.FileWriter;
import java.util.Properties;
import java.util.Set;

public class PropertiesTest {

    /**
     * 正常使用Properties 与Map是类似的
     * 不能定义泛型，所有的key和value都必须是字符串
     */
    public static void useProperties(){

        Properties prop = new Properties();

        prop.setProperty("name","tom");
        prop.setProperty("age","20");
        prop.setProperty("gender","男");

        System.out.println(prop);
        System.out.println(prop.getProperty("age"));
        System.out.println(prop.getProperty("address","哈尔滨"));

        Set<String> keys = prop.stringPropertyNames();
        System.out.println(keys);

        keys.stream()
                .forEach(key-> System.out.println(key+"==="+prop.get(key)));

    }

    /**
     * 此类与文件进行整合使用
     */
    public static void usePropertiesByFile() throws Exception{
        Properties prop = new Properties();

        prop.setProperty("name","tom");
        prop.setProperty("age","20");
        prop.setProperty("gender","男");

        prop.store(new FileWriter("E://a.txt"),"aaaaaa");

        //prop加载 a.txt 文件的内容
        prop.load(new FileReader("E://a.txt"));
        Set<String> keys = prop.stringPropertyNames();
        System.out.println(keys);

        keys.stream()
                .forEach(key-> System.out.println(key+"==="+prop.get(key)));


    }

    public static void main(String[] args) throws Exception {
//        useProperties();
        usePropertiesByFile();
    }


}
